﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using imaginecup.information;
using imaginecup.objectManager;
using Imaginecup.gameState.malaria.componentInit;
using Imaginecup.util;
using Imaginecup.gameState.malaria.manager;
using Microsoft.Xna.Framework;
using Imaginecup.util.collision;
using Imaginecup.util.script.malaria;
using Imaginecup.gameState.malaria.component.entity.effect;

namespace Imaginecup.gameState.malaria.component.entity.item
{
    public class CmpShieldItem : BaseItemEntityComponent
    {
        private List<ICmpChar> m_ChildrenList;

        private double m_ExistTime;
        private int m_RequireMoney;


        public CmpShieldItem(GObjectID a_GID, ObjectManager objMan)
            : base(a_GID, objMan, ComponentTypeID.CID_ITEM_ENTITY)
        {
            m_ChildrenList = new List<ICmpChar>();

            m_ExistTime = 10;
            m_RequireMoney = 3;


//#if WINDOWS
//            m_ExistTime = ScriptReader.GetScriptReader().GetDataToDouble("모기장유지시간");
//            m_RequireMoney = ScriptReader.GetScriptReader().GetDataToInt("모기장돈");
//#endif 
        }

        public override EMessageResult HandleMessage(GMessage a_Msg)
        {
            return base.HandleMessage(a_Msg);
        }

        public override void DisableItem()
        {
            base.DisableItem();
        }

        public override bool IsUseAble()
        {
            if (!IsSelectAble()) return false; 
            IMouseInputHandler input = MouseInputHandler.GetInputHandler();
            if (input.IsOneClick())
            {
                GMessage message = new GMessage(MessageTypeID.MT_CHECK_STATE);
                //마우스 충돌이 일어난 엔티티들을 등록할 수 있다.
                message.m_Data = m_ChildrenList;

                m_ObjectMan.BroadCastMessage(message, ComponentTypeID.CID_CHILDREN_ENTITY);

                if (m_ChildrenList.Count > 0)
                {
                    return true;
                }
            }
            return false;
        }

        public override  bool IsStateEnd()
        {
            IMouseInputHandler input = MouseInputHandler.GetInputHandler();
            Vector2 screen = MalariaState.GetMalariaState().GetMainScreenSize();

            if (input.IsOneClick() && Collision.ContainPoint(Vector2.Zero, screen, input.Position))
                return true;
            return false;
        }

        public override void UseItem(GameTime gameTime)
        {
            m_ChildrenList[0].SetShieldTime(m_ExistTime);
            m_ChildrenList[0].StartShieldItem();

            ShieldEffect effect = new ShieldEffect(new GObjectID(), m_ObjectMan, m_ChildrenList[0]);
            m_ObjectMan.AddComponent(effect);
            
            m_ChildrenList.Clear();

            GameResourceMan.GetResourceMan().AddMoney(-m_RequireMoney);
        }

        public override bool IsSelectAble()
        {
            if (GameResourceMan.GetResourceMan().GetMoney() > m_RequireMoney)
            {
                return true;
            }

            return false;
        }
    }
}
